import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface IHeroState {

}

export interface HeroModelType {
  namespace: 'hero';
  state: IHeroState;
  reducers: {
    changeHeroInfo: ImmerReducer<IHeroState>,
    addSkin: ImmerReducer<IHeroState>,
    delSkin: ImmerReducer<IHeroState>,
    changeSkin: ImmerReducer<IHeroState>,
    changeIntroduce: ImmerReducer<IHeroState>,
    equipRecommend: ImmerReducer<IHeroState>,
    changeSkills: ImmerReducer<IHeroState>,
    addSkills: ImmerReducer<IHeroState>,
    delSkills: ImmerReducer<IHeroState>,
    pushItem: ImmerReducer<IHeroState>,
    delItem: ImmerReducer<IHeroState>,
    changeItem: ImmerReducer<IHeroState>
  };
}

const HeroModel: HeroModelType = {
  namespace: 'hero',
  state: {
    name: "",  
    title: "",
    cate: [],
    scores: { 
      difficulty: 0,
      skill: 0,
      attack: 0,
      survive: 0
    },
    avatar: "",
    banner: "",
    photo: "",
    skins: [],
    shipin: {},
    downWind: {},
    upWind: {},
    skills: [],
    partners: [],
    restrained: [],
    restraint: []
  },
  reducers: {
    changeHeroInfo(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      if (action.classify === 'scores') {
        NewState.scores[action.coms] = action.value
      } else {
        NewState[action.classify] = action.value
      }
      return NewState
    },
    addSkin(state) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState.skins.push({
        name: "",
        img: ""
      })
      return NewState
    },
    delSkin(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState.skins.splice(action.index, 1)
      return NewState
    },
    delSkills(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState.skills.splice(action.index, 1)
      return NewState
    },
    changeSkin(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState.skins[action.index][action.classify] = action.value
      return NewState
    },
    changeIntroduce(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState.shipin[action.classify] = action.value
      return NewState
    },
    equipRecommend(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState[action.classify][action.field] = action.value
      return NewState
    },
    addSkills(state) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState.skills.push({})
      return NewState
    },
    changeSkills(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState.skills[action.index][action.classify] = action.value
      return NewState
    },
    pushItem(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState[action.classify].push({})
      return NewState
    },
    delItem(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      NewState[action.classify].splice(action.index, 1)
      return NewState
    },
    changeItem(state, action) {
      let NewState = JSON.parse(JSON.stringify(state))
      console.log(NewState[action.classify])
      NewState[action.classify][action.index][action.com] = action.value
      return NewState
    }
  }
}

export default HeroModel;